import 'react-native-gesture-handler'
import React, { useEffect, useState } from 'react'
import { StatusBar, Platform } from 'react-native'
import { Provider } from 'react-redux'
import { Root } from 'native-base'
import { NavigationContainer } from '@react-navigation/native'
import {
  createStackNavigator,
  CardStyleInterpolators
} from '@react-navigation/stack'
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'
import storage from '@/libs/storage'
import { navigationRef } from '@/libs/rootNavigation'
import JPush from 'jpush-react-native'
import * as WeChat from 'react-native-wechat-lib'
import JShareModule from 'jshare-react-native'

if (Platform.OS === 'android') {
  StatusBar.setTranslucent(true)
}
if (Platform.OS !== 'android') {
  JShareModule.setup()
}

global.storage = storage

global.storage
  .load({
    key: 'loginState'
  })
  .then(ret => {
  }).catch(() => {
    global.storage.save({
      key: 'loginState',
      data: {}
    }).then()
  })

// tabbar图标样式组件
import TabIcon from './components/TabIcon'
// tab pages
import Home from './pages/Home'
import Prediction from './pages/Prediction'
import Live from './pages/Live'
import UserCenter from './pages/UserCenter'
import LifeRoom from './pages/LifeRoom'

// 新闻详情
import NewsDetail from './pages/NewsDetail'
import Webview from './pages/Webview'

// 系统设置模块
import Setting from './pages/Setting'
import Feedback from './pages/Feedback'

// 收藏、历史
import Collection from './pages/Collection'
import History from './pages/History'
import MyFeedbacks from './pages/MyFeedbacks'
import ConnectService from './pages/ConectUser'
import MyComments from './pages/MyComments'
import Messages from './pages/Messages'

// 编辑资料
import EditInfo from './pages/EditInfo'
import ChangePsd from './pages/ChangePsd'
import VerifyMobile from './pages/VerifyMobile'
import ChangeMobile from './pages/ChangeMobile'

// 登录注册
import Login from './pages/Login'
import Registe from './pages/Registe'
import BindPhone from './pages/BindPhone'

// 全局loading
import Loading from '@/components/Loading'

import configStore from './store'
import mineActivity from './pages/mineActivity'
import mineContact from './pages/mineContact'



const store = configStore()

// RNBootSplash.show()

const Stack = createStackNavigator()
const Tab = createBottomTabNavigator()

const TabScreens = () => {
  return (
    <Tab.Navigator
      initialRouteName="Home"
      tabBarOptions={{
        activeTintColor: '#D2363A',
      }}


      screenOptions={({ route }) => ({
        cardStyle: {
          backgroundColor: '#fff'
        },
        barStyle: {
          backgroundColor: "#000"
        },



        tabBarIcon: ({ focused }) => {
          // const icon = focused
          //   ? require('../../assets/images/tab_home_active.png')
          //   : require('../../assets/images/tab_home.png');
          return <TabIcon focused={focused} name={route.name} />
        }
      })}
    >
      <Tab.Screen
        name="Home"
        component={Home}
        tabBarLabel="首页"
        options={{
          tabBarLabel: '首页'
        }}
      />
      <Tab.Screen
        name="LifeRoom"
        component={LifeRoom}
        tabBarLabel="生活馆"
        options={{
          tabBarLabel: '生活馆'
        }}
      />
      <Tab.Screen
        name="Prediction"
        component={Prediction}
        options={{
          tabBarLabel: '活动报名'
        }}
      />
      <Tab.Screen
        name="Live"
        component={Live}
        options={{
          tabBarLabel: '赛事直播'
        }}
      />
      <Tab.Screen
        name="UserCenter"
        component={UserCenter}
        options={{
          tabBarLabel: '我的'
        }}
      />
    </Tab.Navigator>
  )
}

const StackScreens = () => {
  return (
    <Stack.Navigator
      screenOptions={{
        cardStyle: {
          backgroundColor: '#fff'
        },
        cardStyleInterpolator: CardStyleInterpolators.forHorizontalIOS
      }}
    >
      <Stack.Screen
        name="Tabs"
        component={TabScreens}
        title="fhui"
        options={{
          headerShown: false,
          cardStyle: {
            backgroundColor: '#fff'
          },
          headerTitle: '返回'
        }}
      />
      <Stack.Screen
        name="Setting"
        component={Setting}
        options={{
          title: '系统设置'
        }}
      />
      <Stack.Screen
        name="Feedback"
        component={Feedback}
        options={{
          title: '意见反馈'
        }}
      />
      <Stack.Screen
        name="ConnectService"
        component={ConnectService}
        options={{
          title: '联系客服'
        }}
      />
      <Stack.Screen
        name="MyFeedbacks"
        component={MyFeedbacks}
        options={{
          title: '我的提交'
        }}
      />
      <Stack.Screen
        name="Collection"
        component={Collection}
        options={{
          title: '我的收藏'
        }}
      />
      <Stack.Screen
        name="History"
        component={History}
        options={{
          title: '历史记录'
        }}
      />
      <Stack.Screen
        name="mineActivity"
        component={mineActivity}
        options={{
          title: '我的赛事'
        }}
      />
      <Stack.Screen
        name="mineContact"
        component={mineContact}
        options={{
          title: '通讯录'
        }}
      />
      <Stack.Screen
        name="MyComments"
        component={MyComments}
        options={{
          title: '我的评论'
        }}
      />
      <Stack.Screen
        name="Messages"
        component={Messages}
        options={{
          title: '我的消息'
        }}
      />
      <Stack.Screen
        name="EditInfo"
        component={EditInfo}
        options={{
          title: '编辑信息'
        }}
      />
      <Stack.Screen
        name="ChangePsd"
        component={ChangePsd}
        options={{
          title: '修改密码'
        }}
      />
      <Stack.Screen
        name="VerifyMobile"
        component={VerifyMobile}
        options={{
          title: '验证手机号'
        }}
      />
      <Stack.Screen
        name="ChangeMobile"
        component={ChangeMobile}
        options={{
          title: '修改手机号'
        }}
      />
      <Stack.Screen
        name="Registe"
        component={Registe}
        options={{
          title: '用户注册'
        }}
      />
      <Stack.Screen
        name="BindPhone"
        component={BindPhone}
        options={{
          title: '绑定手机'
        }}
      />
      <Stack.Screen
        name="NewsDetail"
        component={NewsDetail}
        options={{
          title: ''
        }}
      />
      <Stack.Screen
        name="Webview"
        component={Webview}
        options={{
          title: ''
        }}
      />

      <Stack.Screen
        name="Login"
        mode="modal"
        // headerMode="float"
        component={Login}
        cardStyle={{ backgroundColor: '#fff' }}
        options={{
          headerShown: false,
          gestureEnabled: true,
          title: '用户登录',
          cardStyleInterpolator: CardStyleInterpolators.forFadeFromBottomAndroid
        }}
      />
    </Stack.Navigator>
  )
}

const App = () => {
  // const navigation = useNavigation()

  useEffect(() => {
    JPush.setLoggerEnable(true)
    JPush.init()

    console.log('--------------login------------')
    try {
      WeChat.registerApp('wx2d088ba43d483001', 'https://wap.sctynews.cn/app/');//从微信开放平台申请
    } catch (e) {
      console.error('--------------registerApp-------------------:', e);
    }
  }, [])

  return (
    <Root>
      <StatusBar barStyle="dark-content" backgroundColor="transparent" />
      <Provider store={store}>
        <NavigationContainer ref={navigationRef}>
          <StackScreens />
        </NavigationContainer>
      </Provider>
    </Root>
  )
}
export default App
